package cn.juque.jdfsystem.service.impl;

import cn.juque.common.base.PageInfo;
import cn.juque.jdfsystem.config.JdfConfig;
import cn.juque.jdfsystem.entity.FileInfo;
import cn.juque.jdfsystem.entity.LogInfo;
import cn.juque.jdfsystem.enums.LogOperateTypeEnum;
import cn.juque.jdfsystem.mapper.LogInfoMapper;
import cn.juque.jdfsystem.service.ILogInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>文件操作日志</li>
 * </ul>
 * @date 2023-01-10 18:21:44
 **/
@Service("logInfoService")
public class LogInfoServiceImpl implements ILogInfoService {

    @Resource
    private LogInfoMapper logInfoMapper;

    @Resource
    private JdfConfig jdfConfig;

    /**
     * 分页查询
     *
     * @param pageInfo pageInfo
     * @param logInfo  logInfo
     * @return DataGrid
     */
    @Override
    public List<LogInfo> listInfoWithPage(PageInfo pageInfo, LogInfo logInfo) {
        Page<LogInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<LogInfo> list = this.logInfoMapper.queryInfoList(page, logInfo);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 查询
     *
     * @param logInfo logInfo
     * @return List
     */
    @Override
    public List<LogInfo> listInfoWithoutPage(LogInfo logInfo) {
        return this.logInfoMapper.queryInfoList(logInfo);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return LogInfo
     */
    @Override
    public LogInfo detail(String id) {
        return this.logInfoMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param fileInfo        fileInfo
     * @param operateTypeEnum LogOperateTypeEnum
     */
    @Override
    public void saveInfo(FileInfo fileInfo, LogOperateTypeEnum operateTypeEnum) {
        LogInfo logInfo = new LogInfo();
        logInfo.setFileId(fileInfo.getId());
        logInfo.setFileName(fileInfo.getFileName());
        logInfo.setOperateType(operateTypeEnum.getCode());
        logInfo.setOperateNode(this.jdfConfig.getNodeName());
        logInfo.setOperateIp(this.jdfConfig.getBindIp());
        this.logInfoMapper.insert(logInfo);
    }
}
